//
//  BindCameraViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/7/6.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "BindCameraViewController.h"
#import "AddDoorSystemViewController.h"
@interface BindCameraViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView **/
@property(nonatomic,strong) UITableView *tableView;

/** indexTag **/
@property(nonatomic,assign) NSInteger indexTag;

@property(nonatomic,strong) NSMutableArray *searchedData;


/** placeLabel **/
@property(nonatomic,strong) UILabel *placeLabel;

@end

@implementation BindCameraViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.searchedData = [NSMutableArray array];
    [[AppDelegate globalDelegate] loadDeviceFromDatabase];
    
    [self.searchedData addObjectsFromArray:[[AppDelegate globalDelegate] loadDeviceFromDatabase:[NSString stringWithFormat:@"%d",0]]];
    [self.searchedData addObjectsFromArray:[[AppDelegate globalDelegate] loadDeviceFromDatabase:[NSString stringWithFormat:@"%d",1]]];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationStyle];
    
}

- (void)setNavigationStyle{
    
    [self setNavigationTitle:@"摄像头列表" titleColor:nil];
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setRightButtonImage:[UIImage imageNamed:@"icon_add_nor"]];

}

- (void)onRightButtonClick:(id)sender{
    
    AddDoorSystemViewController *vc = [[AddDoorSystemViewController alloc] init];
    vc.indexView = 1;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.searchedData.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Camera *camera = self.searchedData[indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#56504f"];
        [cell.textLabel setFont:[UIFont systemFontOfSize:15.f]];

    }
    cell.textLabel.text = camera.name;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    
    cell.accessoryView = imageView;
    
   
    if ([EXRACT_OBJECT([AppDelegate globalDelegate].doorWiFiModel.macAddress) isEqualToString:camera.uid]) {
        
        imageView.image =[UIImage imageNamed:@"icon_online"];
        
    }else{
        
        imageView.image =[UIImage imageNamed:@"video_icon_notchoose-"];
    }
    
 
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 20;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    return self.placeLabel;
}

#pragma mark - --------------------------UITableView delegate--------------------------


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Camera *camera = self.searchedData[indexPath.row];
    if ([EXRACT_OBJECT([AppDelegate globalDelegate].doorWiFiModel.macAddress) isEqualToString:camera.uid]) {
        
        SAVE_OBJECT(nil, [AppDelegate globalDelegate].doorWiFiModel.macAddress);

    }else{
        
        SAVE_OBJECT(camera.uid, [AppDelegate globalDelegate].doorWiFiModel.macAddress);

    }
    
    NSLog(@"====%@",EXRACT_OBJECT([AppDelegate globalDelegate].doorWiFiModel.macAddress));

    [tableView reloadData];
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
    
}

- (UILabel *)placeLabel{
    
    if (!_placeLabel) {
        
        _placeLabel = [[UILabel alloc] init];
        _placeLabel.text = @"选中摄像头后请到家居入户门智能系统中进行wifi设置";
        _placeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _placeLabel;
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(superView);
        
    }];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
